package com.zhz.company.controller;

import com.zhz.common.controller.BaseController;
import com.zhz.common.tool.mybatisplus.base.core.contract.BaseRequest;
import com.zhz.common.tool.mybatisplus.base.core.contract.Page;
import com.zhz.common.tool.mybatisplus.base.core.contract.R;
import com.zhz.company.biz.DepartBizService;
import com.zhz.company.contract.request.*;
import com.zhz.company.contract.response.DepartmentFindAllResponse;
import com.zhz.company.contract.response.DepartmentFindByCodeResponse;
import com.zhz.company.contract.response.DepartmentFindByIdResponse;
import com.zhz.model.company.contract.param.DepartmentQueryParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhouhengzhe
 * @date 2023/3/22
 */
@Api(value = "部门控制器",tags = "部门控制器")
@RestController
@RequestMapping("/company/departments")
public class DepartmentController extends BaseController {

    @Autowired
    private DepartBizService departBizService;

    @ApiOperation("保存部门")
    @PostMapping("/save")
    public R<Void> save(@RequestBody @Validated(BaseRequest.Save.class) DepartmentSaveRequest departmentSaveRequest) {
        //从父类中获取
        departmentSaveRequest.setCompanyId(companyId);
        departBizService.save(departmentSaveRequest);
        //3.构造返回结果
        return R.ok();
    }

    @ApiOperation("指定企业id查询企业的部门列表")
    @PostMapping("/findAll")
    public R<DepartmentFindAllResponse> findAll() {
        return R.ok(departBizService.findAll(companyId));
    }

    @ApiOperation("根据ID查询department")
    @PostMapping("/findById")
    public R<DepartmentFindByIdResponse> findById(@RequestBody @Validated DepartmentFindByIdRequest departmentFindByIdRequest) {
        return R.ok(departBizService.findById(departmentFindByIdRequest));
    }

    @ApiOperation("修改Department")
    @PostMapping("/update")
    public R<Void> update(@RequestBody @Validated(BaseRequest.Update.class) DepartmentUpdateRequest departmentUpdateRequest) {
        //1.设置修改的部门id
        departmentUpdateRequest.setCompanyId(companyId);
        //2.调用service更新
        departBizService.update(departmentUpdateRequest);
        return R.ok();
    }

    @ApiOperation("根据id删除部门")
    @PostMapping("/delete")
    public R<Void> delete(@RequestBody @Validated(BaseRequest.Delete.class) DepartmentDeleteRequest departmentDeleteRequest) {
        departBizService.deleteById(departmentDeleteRequest);
        return R.ok();
    }

    @ApiOperation("根据code码和公司Id获取部门信息")
    @PostMapping("/search")
    public R<DepartmentFindByCodeResponse> findByCode(@RequestBody @Validated DepartmentFindByCodeRequest departmentFindByCodeRequest) {
        departmentFindByCodeRequest.setCompanyId(companyId);
        return R.ok(departBizService.findByCode(departmentFindByCodeRequest));
    }

    @ApiOperation("分页查询部门信息")
    @PostMapping("/page")
    public R<Page<DepartmentFindByIdResponse>> page(@RequestBody DepartmentQueryParam departmentQueryParam){
        departmentQueryParam.setCompanyId(companyId);
        return R.ok(departBizService.page(departmentQueryParam));
    }
}